package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.adapter.ShopGoodsListAdapter
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.ShopGoodsItemEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.ShopGoodsListContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.ShopGoodsListPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ActivityCloseUtils
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_shop_goods_list.*

/**
 * Created by JacobHHH on 2018/4/21.
 * 商家商品列表
 */
class ShopGoodsListActivity :BaseMvpActivity<ShopGoodsListContract.Model,ShopGoodsListContract.View,ShopGoodsListPresenter>(),ShopGoodsListContract.View{


    private lateinit var mAdapter : ShopGoodsListAdapter
    private lateinit var mShop_id : String
    private var currentPage : Int =1
    private lateinit var mShop_name : String

    override fun createPresenter(): ShopGoodsListPresenter = ShopGoodsListPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_shop_goods_list

    override fun initData() {
        mShop_id = intent.getStringExtra("shop_id")
        mShop_name=intent.getStringExtra("shop_name")
        initView()

        getPresenter().loadData(mShop_id,currentPage.toString(),false)

    }

    private fun initView() {
        tv_title.text=mShop_name
        rec_shop_goods.layoutManager=LinearLayoutManager(mContext)
        mAdapter = ShopGoodsListAdapter(R.layout.item_shop_goods_list, ArrayList())
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN)
        rec_shop_goods.adapter=mAdapter
        mAdapter.setOnLoadMoreListener {
            getPresenter().loadData(mShop_id,currentPage.toString(),true)
        }
        ActivityCloseUtils.getInstance()!!.finishActivity(this,rec_shop_goods)
    }

    override fun initEvent() {
        rec_shop_goods.addOnItemTouchListener(object : OnItemClickListener(){
            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
                val intent = Intent(mContext, GoodsDetailsActivity::class.java)
                intent.putExtra("good_id",mAdapter.data[position].id)
                startActivityForResult(intent,Constants.ACTIVITY_FINISH_REQUEST_CODE)
                (mContext as Activity).overridePendingTransition(R.anim.translate_in,R.anim.scale_out2)
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==Constants.ACTIVITY_FINISH_REQUEST_CODE){
            if (resultCode==Constants.ACTIVITY_FINISH_RESULT_CODE){
                finish()
            }
        }
    }

    override fun setData(data: List<ShopGoodsItemEntity>, isLoadMore: Boolean) {

        fl_loading.visibility= View.GONE
        currentPage++
        var datas =data
        if (!isLoadMore){
            mAdapter.setNewData(datas)
        }else{
            if (data!=null&& data!!.size>0){
                mAdapter.loadMoreComplete()
                mAdapter.addData(datas)
            }else{
                mAdapter.loadMoreEnd()
            }
        }
    }


    override fun onDataFailure(err: String?) {
        fl_loading.visibility= View.GONE
        mAdapter.loadMoreFail()
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCloseUtils.getInstance()!!.detach()
    }

}